package mfuny.ggongal.admin.http;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

import mfuny.ggongal.admin.Server;
import mfuny.ggongal.admin.json.JsonCreater;

import org.json.simple.JSONObject;

import com.ibatis.sqlmap.client.SqlMapClient;

public class HttpNotice {
	SqlMapClient sqlMapper;
	JsonCreater jc;
	
	public HttpNotice() throws Exception {
		sqlMapper = Server.getSqlMapper();
		jc = new JsonCreater();
	}
	
	//공지사항 제목글들 가져오기.
	@SuppressWarnings("unchecked")
	public JSONObject notice_getSubjects(Object startObj) throws Exception {
		int start = startObj != null ? Integer.parseInt((String)startObj) : 0;
		List<Object> notice = sqlMapper.queryForList("notice_selectSubject", start);
		for( int i = 0; i < notice.size(); i++ ) {
			HashMap<String, Object> map = (HashMap<String, Object>)notice.get(i);
			Timestamp ts = (Timestamp) map.get("regdate");
			map.put("regdate", new SimpleDateFormat("yyyy-MM-dd").format(ts));
			notice.set(i, map);
		}
		JSONObject json = jc.getJsonByMapList(notice, "notice_subjects");
		if( notice.size() > 0 ) {
			json.put("responseCode", 1);
		} else {
			json.put("responseCode", 0);
		}
		return json;
	}
	
	//공지사항 특정글 내용 가져오기.
	@SuppressWarnings("unchecked")
	public JSONObject notice_getContent(int notice_no) throws Exception {
		sqlMapper.update("notice_addCount", notice_no);
		HashMap<String, Object> notice = (HashMap<String, Object>) sqlMapper.queryForObject("notice_selectByNoForMap", notice_no);
		Timestamp ts = (Timestamp)notice.get("regdate");
		notice.put("regdate", new SimpleDateFormat("yyyy-MM-dd").format(ts));
		notice.remove("updatetime");
		JSONObject json = jc.getJsonByMap(notice);
		return json;
	}
	
	//마지막 공지사항 글 가져오기.
	@SuppressWarnings("unchecked")
	public JSONObject notice_getFinalContent() throws Exception {
		String notice = (String) sqlMapper.queryForObject("notice_selectFinalContent", null);
		JSONObject json = new JSONObject();
		json.put("notice", notice);
		return json;
	}
}
